package com.zte.shopping.service.impl;

import com.zte.shopping.constant.StatusConstant;
import com.zte.shopping.entity.ProductType;
import com.zte.shopping.entity.User;
import com.zte.shopping.exception.LoginNameOrPasswordErrorException;
import com.zte.shopping.exception.RequestParameterException;
import com.zte.shopping.exception.UserExistException;
import com.zte.shopping.mapper.user.UserMapper;
import com.zte.shopping.service.UserService;
import com.zte.shopping.util.Constants;
import com.zte.shopping.util.MD5Util;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @program: ssm
 * @description: 对User表调用Mapper操作
 * @author: zxb
 * @create: 2020-06-23 15:37
 **/
@Service
public class UserServiceImpl implements UserService {

    private  static  final Logger logger= Logger.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;


    @Override
    public Boolean findUserByLoginName(String loginName) {
        boolean flag = false;
        int i = this.userMapper.findUserByLoginName(loginName);
        if (i > 0) {
            flag = true;
        }
        logger.debug("UserServiceImpl--->findUserByLoginName param:loginName="+loginName+" i="+i+" flag="+flag);
        return flag;
    }

    @Override
    public User userLogin(String loginName, String password) {
        return null;
    }

    @Override
    public List<User> userList(Integer pageIndex) {

        int pageSize = Constants.pageSize;
        int from = (pageIndex-1)*pageSize;
        logger.debug("enter UserServiceImpl ------> userList param: pageIndex="+pageIndex+" pageSize="+pageSize+" from="+from);
        return userMapper.userList(from,pageSize);
    }

    @Override
    public int userListCount() {
        logger.debug("enterUserServiceImpl ------> userListCount");
        return userMapper.userListCount();
    }

    @Override
    public List<User> searchUserByParmaList(Integer pageIndex, String userName, String loginName, String phone, String address, Integer isValid) {
        int pageSize = Constants.pageSize;
        int from = (pageIndex-1)*pageSize;
        logger.debug("enter UserServiceImpl ------>searchUserByParmaList params: pageIndex="+pageIndex+" pageSize="+pageSize+" from="+from
        +" userName="+userName+" loginName="+loginName+" phone="+phone+" address="+address+" isValid="+isValid);
        return userMapper.searchUserByParmaList(from,pageSize,userName,loginName,phone,address,isValid);
    }

    @Override
    public int userListCountByParamList(String userName, String loginName, String phone, String address, Integer isValid) {
        logger.debug("enter UserServiceImpl ------>userListCountByParamList params: userName="+userName+
                " loginName="+loginName+" phone="+phone+" address="+address+" isValid="+isValid);
        return userMapper.userListCountByParamList(userName,  loginName,  phone,  address,  isValid);
    }

    @Override
    public void modifyStatus(String id, String status)throws RequestParameterException {
        logger.debug("enter UserServiceImpl ------>modifyStatus params: id="+id+" status="+status);
        int userStatus = Integer.parseInt(status);

        if (userStatus == StatusConstant.USER_STATUS_ENABLE)  // 如果  会员账号状态为有效(启用)   则改为失效(禁用)
        {
            userStatus = StatusConstant.USER_STATUS_DISABLE;
        } else                                                // 如果  会员账号状态为失效(禁用)   则改为有效(启用)
        {
            userStatus = StatusConstant.PRODUCT_TYPE_STATUS_ENABLE;
        }
        logger.debug("enter UserServiceImpl =============>modifyStatus params: id="+id+" status="+status
                +"====传入下一层参数id="+id+" status="+userStatus);
        userMapper.modifyStatus(Integer.valueOf(id),Integer.valueOf(userStatus));
    }

    @Override
    public void modifyById(User user) {
        // 根据  会员id  与  账号   查询符合条件的会员信息
//        User selectUser = userMapper.selectByIdAndLoginName(user.getUserId(), user.getLoginName());
//
//        if (selectUser != null)
//        {
//            throw new UserExistException("该会员已经存在");
//        }
        logger.debug("enter UserServiceImpl =============>modifyStatus params: userId="+user.getUserId()+" userName="+user.getUserName()
               );
        userMapper.updateById(user);
    }

    /**
     * @param loginName
     * @param password
     * @param code
     * @param session
     * @return com.zte.shopping.entity.User
     * @Author zuti
     * @Description //TODO 会员登录
     * @Date 16:29 2020/6/30
     * @Param [loginName, password, code, session]
     */
    @Override
    public User login(String loginName, String password, String code, HttpSession session) throws LoginNameOrPasswordErrorException {

        // 根据  账号    密码  是否有效    查询对应的会员信息
        User user = userMapper.selectByLoginNameAndPassword(loginName, MD5Util.md5(password), StatusConstant.USER_STATUS_ENABLE);

        if (user == null)
        {
            throw new LoginNameOrPasswordErrorException("账号或者密码错误");
        }

        return user;
    }

    /**
     * @param user
     * @return void
     * @Author zuti
     * @Description //TODO 会员注册
     * @Date 16:36 2020/6/30
     * @Param [user]
     */
    @Override
    public void regist(User user) throws UserExistException {
        // 根据   账号   查询会员信息
        User selectUser = userMapper.selectByLoginName(user.getLoginName());

        if (selectUser != null)
        {
            throw new UserExistException("该账号已被使用");
        }

        // 注册时要对密码进行加密
        user.setPassword(MD5Util.md5(user.getPassword()));

        // 默认新注册的用户均为有效用户
        user.setIsValid(StatusConstant.USER_STATUS_ENABLE);

        // 默认注册的时间为当前时间
        user.setRegistDate(new Date());

        // 添加会员信息
        userMapper.insertUser(user);
    }

    /**
     * @param session
     * @return void
     * @Author zuti
     * @Description //TODO 会员退出
     * @Date 16:41 2020/6/30
     * @Param [session]
     */
    @Override
    public void logout(HttpSession session) {
        session.invalidate();
    }

    /**
     * @param userId
     * @param newPassword
     * @return void
     * @Author zuti
     * @Description //TODO 会员密码修改
     * @Date 9:37 2020/7/1
     * @Param [userId, newPassword]
     */
    @Override
    public void modifyPassword(String userId, String newPassword) {
        userMapper.updatePassword(Integer.parseInt(userId), MD5Util.md5(newPassword));
    }

    /**
     * @param userId
     * @param phone
     * @param address
     * @return void
     * @Author zuti
     * @Description //TODO 前台修改用户信息
     * @Date 9:50 2020/7/1
     * @Param [userId, phone, address]
     */
    @Override
    public void modifyUser(String userId, String phone, String address) {
        userMapper.updateUser(Integer.parseInt(userId), phone, address);
    }


}
